package com.zh.ssmshirofreemarker.controller;

import org.springframework.boot.web.servlet.error.ErrorController;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@Controller
public class MyErrorController implements ErrorController {

    @RequestMapping("/403")
    public String to403(){
        return "403";
    }

    @RequestMapping("/error")
    public String handleError(HttpServletRequest request){
        //获取statusCode:401,404,500
        Integer statusCode = (Integer) request.getAttribute("javax.servlet.error.status_code");
        if(statusCode == 401){
            return "error/401";
        }else if(statusCode == 404){
            return "error/404";
        } else if(statusCode == 400){
            return "error/400";
        } else if(statusCode == 403){
            return "error/403";
        } else{
            return "error/500";
        }
    }

    @Override
    public String getErrorPath() {
        return "/error";
    }

}
